package com.aurora.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.aurora.common.utils.PageUtils;
import com.aurora.gulimall.product.entity.AttrEntity;
import com.aurora.gulimall.product.service.AttrAttrgroupRelationService;
import com.aurora.gulimall.product.service.AttrService;
import com.aurora.gulimall.product.service.CategoryService;
import com.aurora.gulimall.product.vo.AttrGroupRelationVo;
import com.aurora.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.aurora.gulimall.product.entity.AttrGroupEntity;
import com.aurora.gulimall.product.service.AttrGroupService;
import com.aurora.common.utils.R;



/**
 * 属性分组
 *
 * @author aurora
 * @email 244889396@qq.com
 * @date 2022-09-26 10:41:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**关联关系
     * @param attrGroupId
     * @return
     */
//    /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> attrList = attrService.getAttrRelation(attrGroupId);
        return R.ok().put("data",attrList);
    }


    /**未关联关系查询
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params
                            ){
        PageUtils page = attrService.getNoRelationAttr(attrGroupId,params);
        return R.ok().put("page",page);
    }

    /**
     * 新增关联
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }


    /**
     * 删除关联
     * /product/attrgroup/attr/relation
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrService.deleteRelation(attrGroupRelationVo);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long cateLogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,cateLogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCateLogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**获取分组下的所有属性
     * ///product/attrgroup/{catelogId}/withattr
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //1、查出当前分类下的所有属性分组
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        //2、查出每个属性分组的所有属性
        return R.ok().put("data",list);
    }


}
